package com.ssm.stu.controller;

import com.ssm.stu.pojo.Major;
import com.ssm.stu.service.MajorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/majors")
public class MajorController {
    @Autowired
    private MajorService majorService;

    @PostMapping
    public Result insert(@RequestBody Major major) {
        boolean flag = majorService.insertMajor(major);
        Integer code = flag ? Code.POST_OK : Code.POST_ERR;

        return new Result(code, flag);
    }

    @PutMapping
    public Result update(@RequestBody Major major) {
        boolean flag = majorService.updateMajor(major);
        Integer code = flag ? Code.PUT_OK : Code.PUT_ERR;

        return new Result(code, flag);
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable String id) {
        boolean flag = majorService.deleteMajor(id);
        Integer code = flag ? Code.DELETE_OK : Code.DELETE_ERR;

        return new Result(code, flag);
    }

    @GetMapping("/{id}")
    public Result selectById(@PathVariable String id) {
        Major major = majorService.selectMajorById(id);
        boolean flag = major != null;
        Integer code = flag ? Code.GET_OK : Code.GET_ERR;
        String msg = flag ? "" : "查询不到该数据";

        return new Result(code,major,msg);
    }

    @GetMapping
    public Result selectAll() {
        List<Major> majorList = majorService.selectAllMajor();
        boolean flag = majorList != null;
        Integer code = flag ? Code.GET_OK : Code.GET_ERR;
        String msg = flag ? "" : "查询不到数据";

        return new Result(code,majorList,msg);
    }

}
